package com.linjuke.mayouting.common.enums;

import java.util.List;
import java.util.Set;

import com.zjuh.sweet.lang.CollectionUtil;

public enum Permission {
	
	MANAGER_PERMISSION(10, "Role manage", "role/list"),
	MANAGER_USER(20, "User manage", "user/list"),
	CAT_MANAGER(30, "Category manage", "biz/cat/list"),
	ITEM_VERIFY(40, "Item verify", "biz/item/list"),
	
	BAR_CODE_GEN(200, "2-D code gen", "biz/barcode/create"),
	
	;
	
	private Permission(int id, String name, String url) {
		this.name = name;
		this.url = url;
		this.id = id;
	}
	
	private final int id;
	
	private final String name;

	private final String url;
	
	public boolean isAdmin() {
		return id == 1;
	}
	
	public static List<Permission> valueOf(List<Integer> ids) {
		Set<Integer> idsSet = CollectionUtil.newHashSet(ids);
		List<Permission> ret = CollectionUtil.newArrayList();
		for (Permission p : Permission.values()) {
			if (idsSet.contains(p.id)) {
				ret.add(p);
			}
		}
		return ret;
	}
	
	public static Permission valueOf(int id) {
		for (Permission p : Permission.values()) {
			if (p.id == id) {
				return p;
			}
		}
		return null;
	}
	
	public static List<Permission> all() {
		List<Permission> ret = CollectionUtil.newArrayList();
		for (Permission p : Permission.values()) {
			ret.add(p);
		}
		return ret;
	}

	public String getUrl() {
		return url;
	}

	public String getName() {
		return name;
	}

	public int getId() {
		return id;
	}

}
